package l1j.opqlo.NewSystem.Extension;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.NewSystem.A_ColorMessage;
import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.ExpTable;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.Getback;
import l1j.server.server.model.L1EffectSpawn;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.Instance.L1DoorInstance;
import l1j.server.server.model.Instance.L1FieldObjectInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.model.map.L1Map;
import l1j.server.server.serverpackets.S_CharVisualUpdate;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.serverpackets.S_EffectLocation;
import l1j.server.server.serverpackets.S_GreenMessage;
import l1j.server.server.serverpackets.S_MapID;
import l1j.server.server.serverpackets.S_NPCPack;
import l1j.server.server.serverpackets.S_OtherCharPacks;
import l1j.server.server.serverpackets.S_OwnCharPack;
import l1j.server.server.serverpackets.S_PacketBoxGame;
import l1j.server.server.serverpackets.S_RedMessage;
import l1j.server.server.serverpackets.S_RemoveObject;
import l1j.server.server.serverpackets.S_SkillIconGFX;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_Weather;
import l1j.server.server.utils.collections.Lists;

public class IceCave {

	/** 任務進程時間軸 **/
	public class StageRun extends TimerTask {

		private final int _time;
		private final int _type;

		public StageRun(final int type, final int time) {
			this._type = type;
			this._time = time;

		}

		public void begin() {
			GeneralThreadPool.getInstance().schedule(this, this._time); // 延遲時間
		}

		@Override
		public void run() {
			try {
				if (IceCave.this._gameover) {
					this.cancel();
					return;
				}
				switch (this._type) {
				case 1:
					// int x=32870,y=32832; xx=32835 h =6
					IceCave.this.Teleport(32870, 32835, 32831, 6);// 傳送玩家
					IceCave.this.GreenMessage("[副本提示]:打破結界石取得[聖獸擺飾]", false, 0);
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.SmallMob1, 25, 0, 0,
							(short) IceCave.this._mapid, 0, 0, true);// 創出小怪1號
					Thread.sleep(1000);
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.SmallMob2, 30, 0, 0,
							(short) IceCave.this._mapid, 0, 0, true);// 創出小怪2號
					Thread.sleep(1000);
					TreasureMaster.getInstance().spawn(
							IceCave.this.quesiItemId, IceCave.this.Stone, 1,
							32743, 32833, (short) IceCave.this._mapid, 0, 0,
							true);// 創出結界石
					IceCave.this._StageOverTime = new TotalTimer(1800, 0);
					IceCave.this._StageOverTime.begin();
					break;
				case 2:// 結界石被打破後出現提示
					IceCave.this.GreenMessage("[副本提示]:請將[聖獸擺飾]擺放在小魔法陣上。",
							false, 0);
					break;
				case 3:// 下方裝置擺入
					IceCave.this
							.GreenMessage("[副本提示]:你感覺到有人正在招喚你...", false, 0);
					IceCave.this.MobTeleport();
					Thread.sleep(5000);
					IceCave.this.StopBarSet();// 障礙物設置
					IceCave.this.Teleport(32834, 32835, 32831, 2);// 傳送玩家
					IceCave.this.StageMaster(4, 5);
					break;
				case 4:// 下方BOSS出現
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.DownBoss, 1, 32845, 32832,
							(short) IceCave.this._mapid, 0, 0, true);// 創出BOSS
					IceCave.this.RedMessage("[冰魔]:冒險者...滾出去，這不是你該來的地方。", false,
							0);
					IceCave.this._BossCount++;
					break;
				case 5: // 冰魔被殺死後
					IceCave.this.StopBarRemove();// BOSS死亡移除障礙物
					IceCave.this.GreenMessage("[副本提示]:打破結界石取得[聖獸擺飾]", false, 0);
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.SmallMob1, 25, 0, 0,
							(short) IceCave.this._mapid, 0, 0, true);// 創出小怪1號
					Thread.sleep(1000);
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.SmallMob2, 30, 0, 0,
							(short) IceCave.this._mapid, 0, 0, true);// 創出小怪2號
					Thread.sleep(1000);
					TreasureMaster.getInstance().spawn(
							IceCave.this.quesiItemId, IceCave.this.Stone, 1,
							32743, 32833, (short) IceCave.this._mapid, 0, 0,
							true);// 創出結界石
					break;

				case 6:// 上方裝置擺入
					IceCave.this
							.GreenMessage("[副本提示]:你感覺到有人正在招喚你...", false, 0);
					IceCave.this.MobTeleport();
					Thread.sleep(5000);
					IceCave.this.StopBarSet();// 障礙物設置
					IceCave.this.Teleport(32834, 32831, 32835, 2);// 傳送玩家
					IceCave.this.StageMaster(7, 5);
					break;

				case 7:// 上方BOSS出現
					TreasureMaster.getInstance().spawn(0, IceCave.this.UpBoss,
							1, 32845, 32832, (short) IceCave.this._mapid, 0, 0,
							true);// 創出BOSS
					IceCave.this.RedMessage("[冰之女皇]:是誰這麼大膽!?...我要讓你們永遠無法離開這裡。",
							false, 0);
					IceCave.this._BossCount += 2;
					break;

				case 8: // 冰女被殺死後
					IceCave.this.StopBarRemove();// BOSS死亡移除障礙物
					IceCave.this.GreenMessage("[副本提示]:打破最後的結界石取得[聖獸擺飾]", false,
							0);
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.SmallMob1, 25, 0, 0,
							(short) IceCave.this._mapid, 0, 0, true);// 創出小怪1號
					Thread.sleep(1000);
					TreasureMaster.getInstance().spawn(0,
							IceCave.this.SmallMob2, 30, 0, 0,
							(short) IceCave.this._mapid, 0, 0, true);// 創出小怪2號
					Thread.sleep(1000);
					TreasureMaster.getInstance().spawn(
							IceCave.this.quesiItemId, IceCave.this.Stone, 1,
							32743, 32833, (short) IceCave.this._mapid, 0, 0,
							true);// 創出結界石
					break;

				case 9:// 中間結界石被打破後提示
					IceCave.this.GreenMessage("[副本提示]:請將最後的[聖獸擺飾]擺放在大魔法陣上。",
							false, 0);
					break;

				case 10:// 中間裝置擺入
					IceCave.this._waitTime = 22;// 整體計時等候23秒
					IceCave.this.StopBarSet();// 障礙物設置
					IceCave.this.MobTeleport();
					IceCave.this.RedMessage(
							"[奇怪的聲音]:...宙斯!!給我滾出來...你是無法把我關在這的!", false, 0);
					IceCave.this.StageMaster(11, 4);
					break;

				case 11:
					IceCave.this.RedMessage("[奇怪的聲音]:喔...原來是有人放我出來的，讓我看看是誰...",
							false, 0);
					IceCave.this.StageMaster(12, 4);
					break;
				case 12:
					IceCave.this
							.GreenMessage("[副本提示]:你感覺到有人正在招喚你...", false, 0);
					IceCave.this.StageMaster(13, 4);
					break;
				case 13:
					IceCave.this.Teleport(32834, 32831, 32835, 2);// 傳送玩家
					// 中間BOSS出現前準備
					IceCave.this.RedMessage(
							"[奇怪的聲音]:是這幾隻小猴子嗎?...看來我需要好好的招待你們了!", false, 0);
					IceCave.this.StageMaster(14, 4);
					break;
				case 14:
					IceCave.this.GreenMessage("[副本提示]:注意!!!感受到強烈的魔法能量正在影響磁場。",
							false, 0);
					IceCave.this.StageMaster(15, 4);
					break;
				case 15:
					IceCave.this.Effect();
					IceCave.this.StageMaster(16, 1);
					break;
				case 16:
					IceCave.this._Boss = TreasureMaster.getInstance().spawn(0,
							IceCave.this.BigBoss, 1, 32845, 32832,
							(short) IceCave.this._mapid, 0, 0, true)[0];
					IceCave.this._Boss80HP = (int) (IceCave.this._Boss
							.getMaxHp() * 0.8);// 除以2 取得50%HP
					IceCave.this.heal = (int) (IceCave.this._Boss.getMaxHp() / 100D);
					break;
				}
				this.cancel();
			} catch (final Exception e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			}
		}

	}

	/** 全副本時間計時 */
	public class TotalTimer extends TimerTask {
		private final int _time;
		private int _totaltime;

		public TotalTimer(final int totaltime, final int time) {
			this._totaltime = totaltime;
			this._time = time;

		}

		public void begin() {
			final Timer timer = new Timer();
			timer.schedule(this, this._time); // 延遲時間
		}

		@Override
		public void run() {
			try {
				int NextTime = 0;
				while (this._totaltime - 1 > 0) {

					if (IceCave.this._waitTime > 0) {
						IceCave.this._waitTime--;
					} else {
						this._totaltime--;
					}
					if (IceCave.this._Boss != null) {
						if (IceCave.this._Boss.getCurrentHp() <= IceCave.this._Boss80HP) {
							if (NextTime == 0 || NextTime >= this._totaltime) {
								final int x = IceCave.this._Boss.getX(), y = IceCave.this._Boss
										.getY();
								final L1NpcInstance[] _npc = TreasureMaster
										.getInstance().spawn(0,
												IceCave.this.HealNpc, 10, x, y,
												(short) IceCave.this._mapid, 5,
												20000, false);
								IceCave.this._NpcList.clear();
								for (final L1NpcInstance npc : _npc) {
									IceCave.this._NpcList.add(npc);
								}
								NextTime = this._totaltime - 28;
								L1EffectSpawn.getInstance().doSpawnFireRain(
										IceCave.this._Boss);
							}
						}

						if (IceCave.this._NpcList.size() > 0) {
							int truehp = 0;
							for (int i = 0; i < IceCave.this._NpcList.size(); i++) {
								if (IceCave.this._NpcList.get(i).destroyed()) {
									IceCave.this._NpcList.remove(i);
								} else {
									IceCave.this._NpcList
											.get(i)
											.broadcastPacketX8(
													new S_DoActionGFX(
															IceCave.this._NpcList
																	.get(i)
																	.getId(),
															18));
									truehp += IceCave.this.heal;
								}
							}
							if (truehp > 0) {
								IceCave.this._Boss
										.setCurrentHp(IceCave.this._Boss
												.getCurrentHp()
												+ IceCave.this.heal);
								IceCave.this._Boss
										.broadcastPacketX8(new S_SkillSound(
												IceCave.this._Boss.getId(), 832));
							}
						}
					}
					L1PcInstance MsgPc;
					for (int i = 0; i < IceCave.this._pcList.size(); i++) {
						MsgPc = IceCave.this._pcList.get(i);

						if (MsgPc != null) {
							if (MsgPc.getMapId() == IceCave.this._mapid) {
								MsgPc.sendPackets(new S_SkillIconGFX(71,
										this._totaltime));// opqlo 特殊地圖計時顯示

							} else {
								IceCave.this.removePcList(MsgPc);
							}
						}
					}

					Thread.sleep(1000);

				}

				IceCave.this._gameover = true;
				IceCave.this.resetStage();
				for (final String PcName : IceCave.this._pcNameList) {
					TreasureMaster.Game2_pcList.remove(PcName);
				}
				IceCave.this.GreenMessage("副本結束，60秒後所有玩家將自動傳送離場。", true, 60);
				L1PcInstance pc;
				int level;
				for (int i = 0; i < IceCave.this._pcList.size(); i++) {
					pc = IceCave.this._pcList.get(i);
					if (pc != null && !pc.isDead()) {
						if (IceCave.this._BossCount > 3) {// 通過副本增加經驗值
							level = pc.getLevel();
							if (level <= 98 && pc.get_metempsychosis() < 101) {
								pc.setExp(ExpTable.getExpByLevel(level + 1));
								pc.sendPackets(new A_ColorMessage(15,
										"恭喜通關副本，獲得經驗值獎勵。"));
							} else {
								pc.sendPackets(new A_ColorMessage(15,
										"恭喜通關副本，等級已超越獎勵範圍。"));
							}
						}
					}
					Thread.sleep(50);
				}
				int i = 60;
				while (i - 1 > 0) {
					if (IceCave.this._pcList.size() <= 0) {
						break;
					}
					L1PcInstance MsgPc;
					for (int s = 0; s < IceCave.this._pcList.size(); s++) {
						MsgPc = IceCave.this._pcList.get(s);
						if (MsgPc != null) {
							if (MsgPc.getMapId() == IceCave.this._mapid) {
								if (i > 0) {
									MsgPc.sendPackets(new S_SkillIconGFX(71, i));// opqlo
																					// 特殊地圖計時顯示
								}
							} else {
								MsgPc.sendPackets(new S_PacketBoxGame(
										S_PacketBoxGame.STARTTIMECLEAR));
								IceCave.this._pcList.remove(MsgPc);
							}
						}
					}

					Thread.sleep(1000);
					i--;
				}
				IceCave.this.PcGoHome();// 傳送玩家離場
				IceCave.this.clearPet();// 清除殘留的寵物、召喚獸
				TreasureMaster.getInstance()
						.RemoveGameList(IceCave.this._mapid);// 將此地圖設為可使用
				IceCave.this._pcParty = null;
				IceCave.this._pcNameList.clear();
				IceCave.this._pcNameList = null;
				IceCave.this._pcList.clear();
				IceCave.this._pcList = null;
				if (IceCave.this._Stage != null) {
					IceCave.this._Stage.cancel();
					IceCave.this._Stage = null;
				}
				this.cancel();
				IceCave.this._StageOverTime = null;
				IceCave.this._NpcList.clear();
				IceCave.this._NpcList = null;
				IceCave.this._Boss = null;

			} catch (final Exception e) {
				_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			}
		}

	}

	private static Logger _log = Logger.getLogger(IceCave.class.getName());
	private L1PcInstance[] _pcParty;

	public List<String> _pcNameList = new ArrayList<String>();

	private List<L1PcInstance> _pcList = new ArrayList<L1PcInstance>();
	private StageRun _Stage;

	private TotalTimer _StageOverTime;
	public final int _mapid;
	private int _waitTime, _BossCount = 0, _Boss80HP = 0, heal = 0;

	private final int BigBoss, UpBoss, DownBoss, SmallMob1, SmallMob2, Stone,quesiItemId, HealNpc;

	private boolean _gameover;
	private List<L1NpcInstance> _NpcList = new ArrayList<L1NpcInstance>();

	private L1NpcInstance _Boss;

	/** opqlo未完成 BOSS技能、物品掉落相關、NPC對話對應 */
	/** 副本初始設定 */
	public IceCave(final L1PcInstance pc, final int mapid, final boolean isHard) {
		if (isHard) {
			this.BigBoss = 2000215;
			this.Stone = 2000218;
			this.UpBoss = 2000217;
			this.SmallMob1 = 2000219;
			this.DownBoss = 2000216;
			this.SmallMob2 = 2000220;
			this.HealNpc = 2000221;

		} else {
			this.BigBoss = 2000210;
			this.Stone = 2000208;
			this.UpBoss = 2000212;
			this.SmallMob1 = 2000213;
			this.DownBoss = 2000211;
			this.SmallMob2 = 2000214;
			this.HealNpc = 2000221;
		}
		this.quesiItemId = 780022;
		this._mapid = mapid;
		this._pcParty = pc.getParty().getMembers();
		for (final L1PcInstance element : this._pcParty) {
			this.addPcList(element);
			this._pcNameList.add(element.getName());// 紀錄參加玩家的名稱
			TreasureMaster.Game2_pcList.put(element.getId(), this);
		}
		this.GreenMessage("準備進入副本，10秒後將自動傳送。", true, 10);
		this.StageMaster(1, 10);
	}

	/** 列表加入正在副本的玩家 */
	public boolean addPcList(final L1PcInstance pc) {
		if (this._gameover) {
			return false;
		}
		if (this._pcList.contains(pc)) {
			this._pcList.remove(pc);
		}
		this._pcList.add(pc);
		pc.inQuestGame = true;
		return true;
	}

	/** 清除副本殘留的寵物、召喚獸 **/
	private void clearPet() {
		for (final Object obj : L1World.getInstance().getVisibleObjects((short) this._mapid).values()) {
			if (obj instanceof L1SummonInstance) {
				final L1SummonInstance summon = (L1SummonInstance) obj;
				final L1PcInstance pc = (L1PcInstance) summon.getMaster();
				if (pc != null) {
					pc.getPetList().remove(summon.getId());
				}
				summon.deleteMe();
			} else if (obj instanceof L1PetInstance) {
				final L1PetInstance pet = (L1PetInstance) obj;
				final L1PcInstance pc = (L1PcInstance) pet.getMaster();
				if (pc != null) {
					pc.getPetList().remove(pet.getId());
				}
				pet.stopFoodTimer(pet); // 停止飽食度計時
				pet.setDead(true);// 寵物認知
				pet.dropItem();
				pet.deleteMe();
			}
		}
	}

	/** 最終BOSS出現特效 */
	private void Effect() {
		int x = 32826;
		final int y = 32829, y2 = 32838;
		L1PcInstance pc = null;
		for (int i = 0; i < 13; i++) {
			if (i % 2 == 0) {
				continue;
			}
			for (final L1PcInstance ppc : this._pcList) {
				if (ppc != null) {
					ppc.sendPacketsX8(new S_EffectLocation(x, y, 10405));
					ppc.sendPacketsX8(new S_EffectLocation(x, y2, 10405));
					if (pc == null) {
						pc = ppc;
						pc.sendPacketsX8(new S_SkillSound(pc.getId(), 1249));
					}
					break;
				}
			}
			try {
				Thread.sleep(200);
			} catch (final InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			x++;
		}
		pc.sendPacketsX8(new S_EffectLocation(32845, 32832, 761));
		pc.sendPacketsX8(new S_EffectLocation(32845, 32832, 10407));
	}

	/** 傳回副本出王狀態 */
	public int getBossCount() {
		return this._BossCount;
	}

	/** 發送綠色訊息(是否含時間) **/
	private void GreenMessage(final String msg, final boolean haveTime,
			final int Time) {
		for (final L1PcInstance MsgPc : this._pcList) {
			if (MsgPc != null) {
				MsgPc.sendPackets(new S_GreenMessage(msg));
				MsgPc.sendPackets(new A_ColorMessage(15, msg));
				if (haveTime && Time > 0) {
					MsgPc.sendPackets(new S_SkillIconGFX(71, Time));// opqlo
																	// 特殊地圖計時顯示
				}
			}
		}
	}

	/** 將剩餘的小怪傳送進入BOSS房間 */
	private void MobTeleport() {
		try {
			final List<L1MonsterInstance> moblist = Lists.newArrayList();
			for (final Object obj : L1World.getInstance()
					.getVisibleObjects((short) this._mapid).values()) {
				if (obj instanceof L1MonsterInstance) {
					moblist.add((L1MonsterInstance) obj);
				}
			}
			if (moblist.size() <= 0) {
				return;
			}
			int x = 32846, y = 32827;
			final int srcy = 32827;
			L1MonsterInstance mob;
			L1Location location;
			L1Map map;
			for (int i = 0; i < moblist.size(); i++) {
				mob = moblist.get(i);
				mob.getMap().setPassable(mob.getX(), mob.getY(), true);
				mob.setX(x);
				mob.setY(y);
				location = mob.getLocation();
				map = mob.getMap();
				if (!map.isInMap(location) || !map.isPassable(location)) {
					i--;
					if (y < 32840) {
						y++;
					} else {
						x++;
						y = srcy;
					}
					continue;
				}
				mob.setHomeX(x);
				mob.setHomeY(y);
				mob.setHeading(6);

				if (y < 32840) {
					y++;
				} else {
					x++;
					y = srcy;
				}
				mob.broadcastPacketAll(new S_NPCPack(mob)); // TODO
															// 重新發布NPCPack避免傳送後變隱形

				mob.getMap().setPassable(mob.getX(), mob.getY(), false);

				Thread.sleep(10);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	/** 特定NPC死亡後觸發任務 */
	public void npdDead(final int npcid) {

		if (npcid == this.Stone) {
			if (this._BossCount == 3) {
				this.StageMaster(9, 1);
			} else {
				this.StageMaster(2, 1);
			}
		} else if (npcid == this.UpBoss) {
			this.StageMaster(8, 1);
		} else if (npcid == this.DownBoss) {
			this.StageMaster(5, 1);
		} else if (npcid == this.BigBoss) {
			this._BossCount++;
			this._StageOverTime._totaltime = 0;
		}
	}

	/** 將玩家傳出副本地圖 */
	private void PcGoHome() {
		L1PcInstance pc = null;
		List<L1ItemInstance> itemList;
		try {
			for (final Object obj : L1World.getInstance()
					.getVisibleObjects((short) this._mapid).values()) {
				if (obj instanceof L1PcInstance) {
					pc = (L1PcInstance) obj;
					if (pc != null) {

						/** 刪除副本通關道具(禁止帶出) */
						itemList = pc.getInventory().getItems();
						for (final L1ItemInstance item : itemList) {

							if (item.getItem().getType() == 20) {
								pc.getInventory().removeItem(item);
							}
						}

						if (pc.isDead()) {
							this.reStartPlayer(pc);
						} else {
							// 傳送至威頓村

							L1Teleport.teleport(pc, 33710, 32521, (short) 4,
									pc.getHeading(), true);
							this._pcList.remove(pc);
							pc.sendPackets(new S_PacketBoxGame(
									S_PacketBoxGame.STARTTIMECLEAR));// opqlo
																		// 特殊地圖計時顯示
						}
					}
				} else if (obj instanceof L1Inventory) {// 刪除副本內的物品
					((L1Inventory) obj).clearItems();
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	/** 發送紅色訊息 **/
	private void RedMessage(final String msg, final boolean haveTime,
			final int Time) {
		for (final L1PcInstance MsgPc : this._pcList) {
			if (MsgPc != null) {
				MsgPc.sendPackets(new S_RedMessage(msg));
				if (haveTime && Time > 0) {
					MsgPc.sendPackets(new S_SkillIconGFX(71, Time));// opqlo
																	// 特殊地圖計時顯示
				}
			}
		}
	}

	/** 列表移除離開副本的玩家 */
	public void removePcList(final L1PcInstance pc) {
		if (this._pcList.contains(pc)) {
			this._pcList.remove(pc);
		}
		pc.sendPackets(new S_PacketBoxGame(S_PacketBoxGame.STARTTIMECLEAR));
		if (this._pcList.size() <= 0) {
			this._StageOverTime._totaltime = 0;
		}
	}

	/** 重置副本 */
	private void resetStage() {

		for (final Object obj : L1World.getInstance().getVisibleObjects((short) this._mapid).values()) {

			if (obj instanceof L1DoorInstance) {// 門關閉
				((L1DoorInstance) obj).close();

			} else if (obj instanceof L1NpcInstance) {// 刪除副本內的怪物
				final L1NpcInstance npc = (L1NpcInstance) obj;
				if (npc.getMaster() == null) {
					npc.deleteMe();
				}
			} else if (obj instanceof L1FieldObjectInstance) {
				((L1FieldObjectInstance) obj).deleteMe();
			}
		}
	}

	/** 副本內死亡的玩家重新開始 */
	private void reStartPlayer(final L1PcInstance pc) {
		// pc.stopPcDeleteTimer();
		try {
			pc.stopPcDeleteTimer();
			final int[] loc = Getback.GetBack_Location(pc, true);

			pc.removeAllKnownObjects();
			pc.broadcastPacketAll(new S_RemoveObject(pc));

			pc.setCurrentHp(pc.getLevel());
			pc.set_food(40);
			pc.setStatus(0);
			L1World.getInstance().moveVisibleObject(pc, loc[2]);
			pc.setX(loc[0]);
			pc.setY(loc[1]);
			pc.setMap((short) loc[2]);
			pc.sendPackets(new S_MapID(pc.getMapId(), pc.getMap()
					.isUnderwater()));
			pc.broadcastPacketAll(new S_OtherCharPacks(pc));
			pc.sendPackets(new S_OwnCharPack(pc));
			pc.sendPackets(new S_CharVisualUpdate(pc));
			pc.startHpRegeneration();
			pc.startMpRegeneration();
			pc.sendPackets(new S_Weather(L1World.getInstance().getWeather()));
			if (pc.getHellTime() > 0) {
				pc.beginHell();
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	/** 觸發進程任務 **/
	public void StageMaster(final int type, final int time) {
		if (this._gameover) {
			this._Stage.cancel();
			return;
		}
		if (this._Stage != null) {
			this._Stage.cancel();
		}
		this._Stage = new StageRun(type, time * 1000);
		this._Stage.begin();
	}

	/** 障礙物移除 */
	private void StopBarRemove() {
		for (final Object obj : L1World.getInstance()
				.getVisibleObjects((short) this._mapid).values()) {
			if (obj instanceof L1FieldObjectInstance) {
				((L1FieldObjectInstance) obj).deleteMe();

			}
		}
	}

	/** 障礙物設置 */
	private void StopBarSet() {
		// TreasureMaster.getInstance().spawn(0, 45684, 1,
		// 32847,32863,(short)_mapid, 0, 0, true);
		final int npcid = 2000065;// 2000088 2000097 2000068

		final int X = 32816;
		L1NpcInstance npc = null;
		for (int i = 32830; i < 32840; i++) {
			npc = NpcTable.getInstance().newNpcInstance(npcid);
			npc.setId(IdFactory.getInstance().nextId());
			npc.setMap((short) this._mapid);
			npc.setX(X);
			npc.setY(i);
			npc.setHomeX(X);
			npc.setHomeY(i);
			npc.setHeading(0);
			// npc.setPortalNumber(portalNumber);
			npc.getMap().setPassable(X, i, false);// 設定為不能走
			L1World.getInstance().storeObject(npc);
			L1World.getInstance().addVisibleObject(npc);
		}

	}

	/** 傳送玩家 */
	private void Teleport(int x, int y, final int yy, final int head) {
		final int srcy = y;
		L1PcInstance pc;
		for (int i = 0; i < this._pcList.size(); i++) {
			pc = this._pcList.get(i);
			if (!pc.isDead()) {
				L1Teleport.teleport(pc, x, y, (short) this._mapid, head, true);
				if (y <= yy) {
					x--;
					y = srcy;
				}
				y--;
			}
		}
		// L1Teleport.teleport(pc, loc, head, effectable)
	}
}
